﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the L, a, b values are filtered instead of R G B
    /// </summary>
    public class YPbPrFilter : BaseForeBackgroundColorFilter
    {
        protected DoubleInterval m_YInterval = new DoubleInterval(0.0f, 1.0f);
        protected DoubleInterval m_PbInterval = new DoubleInterval(0.0f, 1.0f);
        protected DoubleInterval m_PrInterval = new DoubleInterval(0.0f, 1.0f);

        /// <summary>
        /// Default constructor
        /// </summary>
        public YPbPrFilter()
        {
        }

        /// <summary>
        /// Specify intervals directly
        /// </summary>
        /// <param name="_y">The _y.</param>
        /// <param name="_pb">The _PB.</param>
        /// <param name="_pr">The _PR.</param>
        public YPbPrFilter(DoubleInterval _y, DoubleInterval _pb, DoubleInterval _pr)
        {
            m_YInterval = _y;
            m_PbInterval = _pb;
            m_PrInterval = _pr;
        }

        /// <summary>
        /// Used by clone to create a deep copy
        /// </summary>
        /// <param name="_yPbPrFilter"></param>
        internal YPbPrFilter(YPbPrFilter _yPbPrFilter)
        {
            this.m_YInterval = _yPbPrFilter.m_YInterval;
            this.m_PbInterval = _yPbPrFilter.m_PbInterval;
            this.m_PrInterval = _yPbPrFilter.m_PrInterval;
        }

        /// <summary>
        /// Gets or sets Y
        /// </summary>
        public DoubleInterval Y
        {
            get { return m_YInterval; }
            set { m_YInterval = value; }
        }

        /// <summary>
        /// Gets or sets Pb
        /// </summary>
        public DoubleInterval Pb
        {
            get { return this.m_PbInterval; }
            set { this.m_PbInterval = value; }
        }

        /// <summary>
        /// Gets or sets Pr
        /// </summary>
        public DoubleInterval Pr
        {
            get { return this.m_PrInterval; }
            set { this.m_PrInterval = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            YPbPr hsb = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    hsb = new YPbPr(bitmap.GetPixel(x,y));

                    if (m_YInterval.Contains(hsb.Y)
                        && m_PbInterval.Contains(hsb.Pb)
                        && m_PrInterval.Contains(hsb.Pr))
                    {
                        bitmap.SetPixel(x, y, hsb.ToColor());
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new YPbPrFilter(this.m_YInterval, this.m_PbInterval, this.m_PrInterval);
        }        
    }
}
